package top.bugger168.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.bugger168.content.mapper.CourseTeacherMapper;
import top.bugger168.content.model.po.CourseTeacher;
import top.bugger168.content.service.CourseTeacherService;

import java.util.List;

/**
 * 教师信息服务接口实现类
 */
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<CourseTeacher> listTeacher(long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        return courseTeacherMapper.selectList(queryWrapper);
    }

    @Transactional
    @Override
    public CourseTeacher saveTeacher(CourseTeacher courseTeacher) {
        if(courseTeacher.getId()==null){
            // 增加教师
            CourseTeacher courseTeacherNew = new CourseTeacher();
            BeanUtils.copyProperties(courseTeacher,courseTeacherNew);
            courseTeacherMapper.insert(courseTeacherNew);
            return courseTeacherNew;
        }else{
            // 更新教师信息
            courseTeacherMapper.updateById(courseTeacher);
            return courseTeacher;
        }
    }

    @Transactional
    @Override
    public void removeTeacher(long courseId, long teacherId) {
        courseTeacherMapper.deleteById(teacherId);
    }
}
